package com.wing.order.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.order.model.entity.InventoryInout;
import com.wing.order.model.request.InventoryInoutForm;
import com.wing.order.model.response.InventoryInoutVO;
import com.wing.order.service.InventoryInoutService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author
 */

@Slf4j
@Validated
@Api(value = "Admin-入库单管理", tags = "#/order/inventoryInout")
@RestController("inventoryInoutController")
@RequestMapping("order/admin/inventoryInout")
@RequiredArgsConstructor
public class InventoryInoutController {
    @Autowired
    private final InventoryInoutService inventoryInoutService;
    private final EnterpriseClient enterpriseClient;

    @ApiOperation(value = "查询所有入库单", notes = "搜索")
    @ResponseBody
    @GetMapping("/list")
    public PageResult<InventoryInout> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            InventoryInout InventoryInout){
        IPage<InventoryInout> list = inventoryInoutService.getInventoryInoutPageList(InventoryInout,page,limit);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加入库单", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(InventoryInoutForm.Add.class) InventoryInoutForm inventoryInoutForm) {
        try {
            inventoryInoutService.add(inventoryInoutForm);
            return JsonResult.success("操作成功！");
        } catch (Exception e) {
            log.error("添加入库计划:",e);
            return JsonResult.fail("操作失败:" + e.getMessage());
        }
    }

    @ApiOperation(value = "修改入库单", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(InventoryInoutForm.Update.class) InventoryInoutForm inventoryInoutForm) {
        InventoryInout inventoryInout = inventoryInoutService.getById(inventoryInoutForm.getId());
        if(inventoryInout == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(InventoryInout.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(inventoryInoutForm, inventoryInout, copyOptions);
        if (inventoryInoutService.updateById(inventoryInout)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



@ApiOperation(value = "删除入库单", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inventoryInoutId", value = "入库单id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long inventoryInoutId) {
        if (inventoryInoutService.removeById(inventoryInoutId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
    @ApiOperation(value = "查询入库单详情信息", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "入库单id", required = true, dataType = "Long", paramType = "query")
    })
    @GetMapping("/getInventoryInoutById")
    public JsonResult<InventoryInoutVO> getInventoryInoutById(Long id) {
        InventoryInoutVO info = inventoryInoutService.get4appById(id);
        return JsonResult.success(info);
    }
}
